package com.jww.service.bill;

import com.jww.dao.BaseDao;
import com.jww.dao.bill.BillDao;
import com.jww.dao.bill.BillDaoImpl;
import com.jww.pojo.Bill;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BillServiceImpl implements BillService {

    // 方便调用dao层
    private BillDao billDao;

    public BillServiceImpl() {
        billDao = new BillDaoImpl();
    }

    // 查询订单总数
    @Override
    public int getBillCount(String productName, int providerId, int isPayment) {
        Connection conn = null;
        int count = 0;
        try {
            conn = BaseDao.getConnection();
            count = billDao.getBillCount(conn, productName, providerId, isPayment);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeResource(conn, null, null);
        }
        return count;
    }

    // 查询订单列表信息
    @Override
    public List<Bill> getBillList(String productName, int providerName, int isPayment, int currentPageNo,
            int pageSize) {
        Connection conn = null;
        List<Bill> billList = new ArrayList<Bill>();

        try {
            conn = BaseDao.getConnection();
            billList = billDao.getBillList(conn, productName, providerName, isPayment, currentPageNo, pageSize);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeResource(conn, null, null);
        }
        return billList;
    }

    // 添加订单
    @Override
    public boolean addBill(Bill bill) {
        Connection conn = null;
        boolean flag = false;

        try {
            conn = BaseDao.getConnection();
            conn.setAutoCommit(false);
            int count = billDao.addBill(conn, bill);
            conn.commit();
            if (count > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            try {
                System.out.println("rollback()=========================");
                conn.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeResource(conn, null, null);
        }
        return flag;
    }

    // 查询订单信息
    @Override
    public Bill getBillId(int id) {
        Connection conn = null;
        Bill bill = null;
        try {
            conn = BaseDao.getConnection();
            bill = billDao.getBillId(conn, id);
        } catch (SQLException e) {
            bill = null;
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeResource(conn, null, null);
        }
        return bill;
    }

    // 修改订单信息
    @Override
    public boolean updateBill(Bill bill) {
        Connection conn = null;
        boolean flag = false;
        int updateRows = 0;
        try {
            conn = BaseDao.getConnection();
            conn.setAutoCommit(false);
            updateRows = billDao.updateBill(conn, bill);
            conn.commit();
            if (updateRows > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            try {
                System.out.println("rollback()===========================");
                conn.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeResource(conn, null, null);
        }
        return flag;
    }

    // 根据billCode查询出bill
    @Override
    public Bill selectBillCodeExist(String billCode) {
        Connection conn = null;
        Bill bill = new Bill();
        try {
            conn = BaseDao.getConnection();
            bill = billDao.getBillCode(conn, billCode);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.closeResource(conn, null, null);
        }
        return bill;
    }

    @Override
    public boolean deleteBill(int id) {
        Connection conn = null;
        boolean flag = false;
        try {
            conn = BaseDao.getConnection();
            conn.setAutoCommit(false);
            int updateRows = billDao.deleteBill(conn, id);
            conn.commit();
            if (updateRows > 0) {
                flag = true;
            }
        } catch (SQLException e) {
            try {
                System.out.println("rollback()===========================");
                conn.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }
        return flag;
    }

    // junit测试单元
    // @Test
    // public void Test()
    // {
    // BillService billService = new BillServiceImpl();
    // int billCount = billService.getBillCount(null, 0);
    // System.out.println(billCount);
    // }
}
